﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;


namespace TAMS.Admin
{
    public partial class Admin_HousingModify : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string enteredID;
        static string enteredName;



        protected void Page_Load(object sender, EventArgs e)
        {
            // Connect to the Database
            conn = new MySqlConnection(Resource1.databaseConnection);
            conn.Open();

            ((Menu)Master.FindControl("Menu1")).Items[6].Selected = true;
            Master.FindControl("ContentPlaceholder1").FindControl("Button2").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button3").Visible = false;
            Master.FindControl("ContentPlaceholder1").FindControl("Button4").Visible = false;

            enteredID = Request.QueryString["id"];
            enteredName = Request.QueryString["name"];

            if (!IsPostBack)
            {
                string query = "SELECT* FROM Housing C WHERE c.hid='" + enteredID + "'";
                sqlCommand = new MySqlCommand(query, conn);
                readVals = sqlCommand.ExecuteReader();
                readVals.Read();

                string name = readVals.GetString("h_building");
                int pNo = readVals.GetInt32("h_telNo");
                int dNO = readVals.GetInt32("doorNo");

                ID.Text = ""+dNO;
                Name.Text = name;
                PNO.Text = "" + pNo;
            }

        }

        protected void Add_Click(object sender, EventArgs e)
        {
            // Connect to the Database
            conn = new MySqlConnection(Resource1.databaseConnection);
            conn.Open();

            string query = "UPDATE Housing" +
                " SET doorNo= '" + ID.Text + "'," + "h_building='" + Name.Text + "'," + "h_telno='" + PNO.Text + "'" +
                " WHERE hid='" + enteredID + "'";


            sqlCommand = new MySqlCommand(query, conn);
            sqlCommand.ExecuteNonQuery();

            Response.Redirect("Admin_Housing.aspx");
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_Housing.aspx");
        }
    }
}